<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}

class Index_MxShopPage extends MobilePage {

    //获取短信设置
    protected function getWapSet(){
        global $_W,$_GPC;
        $set = m('common')->getSysset(array('shop','wap'));
        $set['wap']['color']   = empty($set['wap']['color'])?'#fff':$set['wap']['color'];

        $params = array();
        if(!empty($_GPC['mid'])){
            $params['mid'] = $_GPC['mid'];
        }
        if(!empty($_GPC['backurl'])){
            $params['backurl'] = $_GPC['backurl'];
        }
        $set['wap']['loginurl'] = mobileUrl('account/login',$params);
        $set['wap']['regurl'] = mobileUrl('account/register',$params);
        $set['wap']['forgeturl'] = mobileUrl('account/forget',$params);
        return $set;
    }

    /*
     * 获取手机验证码接口
     * @param string phone 手机号码
     * @param string temp 可为空（sms_reg 注册验证码，sms_forget 修改密码验证码）
     * @param string token 令牌
     * @return string code/msg
     */
    public function getRegCode() {
		global $_W, $_GPC;

        @session_start();

        $set = $this->getWapSet();

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token'])){
            show_json(0, '参数有误');
        }

        $mobile = trim($_GPC['phone']);
        $temp = trim($_GPC['temp']);
        $imgcode = trim($_GPC['imgcode']);

        if(empty($mobile) || !is_phone($mobile)){
            show_json(0, '请输入正确的手机号码');
        }
        if(empty($temp)){
            show_json(0, '参数错误');
        }
        if(!empty($_SESSION['verifycodesendtime']) && $_SESSION['verifycodesendtime']+60>time()){
            show_json(0, '请求频繁请稍后重试');
        }

        if(!empty($set['wap']['smsimgcode'])){
            if(empty($imgcode)){
                show_json(0, '请输入图形验证码');
            }
            $imgcodehash = md5(strtolower($imgcode) . $_W['config']['setting']['authkey']);
            if($imgcodehash!=trim($_GPC['__code'])){
                show_json(-1, '请输入正确的图形验证码');
            }
        }

        $member =pdo_fetch('select id,mobile,pwd,salt from '.tablename('mx_shop_user').' where mobile=:mobile limit 1',array(':mobile'=>$mobile));

        if($temp=='sms_forget' && empty($member)){
            show_json(0, "此手机号未注册");
        }
        if($temp=='sms_reg' && !empty($member)){
            show_json(0, "此手机号已注册，请直接登录");
        }

        $sms_id = $set['wap'][$temp];
        if(empty($sms_id)){
            show_json(0, '短信发送失败(NOSMSID)');
        }

        $key = '__mx_shop_user_verifycodesession_'.$_W['uniacid'].'_'.$mobile;

        $code = random(5,true);
        $shopname = $_W['shopset']['shop']['name'];
        $ret = com('sms')->send($mobile, $sms_id, array('验证码'=>$code,'商城名称'=>!empty($shopname) ? $shopname : "商城名称"));
        if($ret['status']){
            $_SESSION[$key] = $code;
            $_SESSION['verifycodesendtime'] = time();
            show_json(1, "短信发送成功");
        }
        show_json(0, $ret['message']);

	}

}
